#[tokio::main(max_threads = 500)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    use std::env;
    let args = env::args().collect::<Vec<String>>() ;

    println!("start...") ;
    let file_num_text: &String = &args[2] ;
    let file_num = file_num_text.parse().unwrap() ;
    let files = (0..file_num).map(|idx| {
        let file_path = format!("larluo_{}.txt", idx.to_string()) ;
        let file = std::fs::OpenOptions::new()
        .write(true)
        .append(true)
        .create(true)
        .open(&file_path).unwrap() ;
        Arc::new(Mutex::new(file))
    }).collect::<Vec<_>>();



    let num_text = &args[1] ;
    let num = num_text.parse::<u64>().unwrap() ;
    println!("num: {:?}", num) ;


    let text = args[3].to_owned() ;
    println!("500!") ;
    for _tid in 0..500 {
        let text_clone = text.clone() ;
        let files_clone = files.clone() ;
        tokio::spawn(async move {
            for _i in 0..num {
                use std::io::Write;
                use rand::prelude::*;
                let mut rng = rand::thread_rng();
                let idx = rng.gen_range(0..file_num) ;

                let one_file = &files_clone[idx] ;
                let mut file = one_file.lock() ;


                file.write_all(text_clone.as_bytes()).unwrap() ;
                file.sync_data().unwrap();
            }
        }) ;
    }
    println!("finished...") ;
    Ok(())
}

